package com.mrjy.integral.bo;

import java.sql.Timestamp;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.mrjy.common.entity.SignInInfo;
import com.mrjy.common.entity.SignUpGraphicInfo;
import com.mrjy.common.entity.UserInfo;
import com.mrjy.common.enums.ResultEnum;
import com.mrjy.common.exception.InnerException;
import com.mrjy.common.util.DateUtil;
import com.mrjy.common.util.ExcelUtil;
import com.mrjy.common.util.GlobalConstant;
import com.mrjy.common.util.ResultUtil;
import com.mrjy.integral.dao.ISignInInfoDao;
import com.mrjy.integral.dao.ISignUpGraphicInfoDao;
import com.mrjy.maintain.dao.IUserInfoDao;

@Service("signUpGraphicInfoBo")
public class SignUpGraphicInfoBo implements ISignUpGraphicInfoBo {

    @Resource
    private ISignUpGraphicInfoDao signUpGraphicInfoDao;
    @Resource
    private ISignInInfoDao signInInfoDao;
    @Resource
    private IUserInfoDao userInfoDao;

    private static Logger logger = LoggerFactory.getLogger(SignUpGraphicInfoBo.class);

    @Override
    public Object txInsert(SignUpGraphicInfo info) {
        Map<String, Object> map = new HashMap<String, Object>();
        info.setAddDate(new Timestamp(System.currentTimeMillis()));
        info.setDisableFlag(0);
        info.setSaveNum(0);
        info.setEditNum(0);
        info.setSpecifiedDate(new Timestamp(DateUtil.stringToLong(info.getSpecifiedDateStr(), DateUtil.FULL_DATE)));

        int count = signUpGraphicInfoDao.insert(info);
        if (count == 1) {
            map.put("err_code", 0);
            map.put("err_msg", "插入成功");
        } else {
            map.put("err_code", 1);
            map.put("err_msg", "插入失败");
        }
        return map;

    }

    @Override
    public Object queryById(int id) {
        Map<String, Object> map = new HashMap<String, Object>();
        SignUpGraphicInfo info = signUpGraphicInfoDao.queryById(id);
        if (info != null) {
            map.put("err_code", 0);
            map.put("err_msg", "查询成功");
            map.put("info", info);
        } else {
            map.put("err_code", 1);
            map.put("err_msg", "无数据");
        }
        return map;
    }

    @Override
    public Object txUpdateById(SignUpGraphicInfo info) {
        Map<String, Object> map = new HashMap<String, Object>();
        if (StringUtils.isNotBlank(info.getText()) && StringUtils.isBlank(info.getPicName())) {
            map.put("err_code", 500);
            map.put("err_msg", "参数错误");
            return map;
        }
        info.setSpecifiedDate(new Timestamp(DateUtil.stringToLong(info.getSpecifiedDateStr(), DateUtil.FULL_DATE)));
        int count = signUpGraphicInfoDao.updateById(info);
        if (count == 1) {
            map.put("err_code", 0);
            map.put("err_msg", "更新成功");
        } else {
            map.put("err_code", 1);
            map.put("err_msg", "更新失败");
        }
        return map;
    }

    @Override
    public Map<String, Object> deleteByIds(int[] ids) {
        int row = signUpGraphicInfoDao.deleteByIds(ids);
        if (row > 0) {
            return ResultUtil.success();
        } else {
            return ResultUtil.error();
        }
    }

    @Override
    public Map<String, Object> updateDisableFlag(SignUpGraphicInfo info) {
        Map<String, Object> map = new HashMap<String, Object>();
        int result = signUpGraphicInfoDao.updateDisableFlag(info);
        if (result == 1) {
            map.put("err_code", 0);
            map.put("err_msg", "更新禁用标识成功");
        } else {
            map.put("err_code", 1);
            map.put("err_msg", "更新禁用标识失败");
        }
        return map;
    }

    @Override
    public Object listPage(SignUpGraphicInfo info) {
        Map<String, Object> map = new HashMap<String, Object>();
        int count = signUpGraphicInfoDao.countListPage(info);
        List<SignUpGraphicInfo> list = signUpGraphicInfoDao.listPage(info);

        HashMap saveMap = new HashMap();

        Map<String, Object> signUpMap = signUpGraphicInfoDao.queryInfo(info);

        SignInInfo signInInfo = new SignInInfo();
        signInInfo.setStartTimeStr(info.getSpecifiedDateStartStr());
        signInInfo.setEndTimeStr(info.getSpecifiedDateEndStr());
        HashMap signInMap = signInInfoDao.queryTotalInfo(signInInfo);

        if (signUpMap != null) {
            saveMap.putAll(signUpMap);
        }
        if (signInMap != null) {
            saveMap.putAll(signInMap);
        }
        saveMap.put("textGraphic", count);

        if (list != null && list.size() > 0) {
            map.put("err_code", 200);
            map.put("err_msg", "查询成功");
            map.put("rows", list);
            map.put("total", count);

            map.put("info", saveMap);
        } else {
            map.put("err_code", 500);
            map.put("err_msg", "无数据");
            map.put("rows", list);
            map.put("total", count);
            map.put("info", saveMap);
        }
        return map;
    }

    /**
     * picType : 1 是用户保存数， 2： 系统内部特定图片 3.系统内部随机图片
     *
     * @param info
     * @param session
     * @return
     */
    @Override
    public Object selectTodayGraphic(SignUpGraphicInfo info, HttpSession session) {
        Map<String, Object> map = new HashMap<String, Object>();
        UserInfo loginUser = (UserInfo) session.getAttribute(GlobalConstant.USER_INFO);
        if (null == loginUser) {
            map.put("err_code", -1);
            map.put("err_msg", "用户未登录");
            return map;
        }
        SignInInfo signInInfo = signInInfoDao.queryByUserId(loginUser.getId());
        if (signInInfo.getPicName() != null) {
            info.setText(signInInfo.getText());
            info.setPicName(signInInfo.getPicName());
            map.put("info", info);
        } else {
            SignUpGraphicInfo signUpGraphicInfo = signUpGraphicInfoDao.selectTodayGraphic(info);
            if (signUpGraphicInfo == null) {
                signUpGraphicInfo = signUpGraphicInfoDao.selectRandomGraphic(info);
            }
            map.put("info", signUpGraphicInfo);
        }
        map.put("err_code", 200);
        map.put("err_msg", "查询成功");
        return map;
    }

    @Override
    public Object export(SignUpGraphicInfo info, HttpServletResponse response) {
        List<SignUpGraphicInfo> dataset = signUpGraphicInfoDao.listPage(info);
        if (CollectionUtils.isEmpty(dataset)) {
            return ResultUtil.error(ResultEnum.NO_DATA_EXPORT);
        }
        try {
            ExcelUtil.writeExcel(response, "签到图文" + DateUtil.dateToString(new Date(), DateUtil.FULL_DATE_STR) + ".xls", dataset, SignUpGraphicInfo.class);
            return ResultUtil.success();
        } catch (Exception e) {
            logger.error("【导出活动】 导出失败,error:{}", e.getMessage());
            throw new InnerException(ResultEnum.ERROR);
        }
    }

    @Override
    public Object txAddSaveNum(SignUpGraphicInfo info) {
        Map<String, Object> map = new HashMap<String, Object>();
        if (info.getId() == null) {
            map.put("err_code", 500);
            map.put("err_msg", "参数错误");
            return map;
        }
        SignUpGraphicInfo signUpGraphicInfo = signUpGraphicInfoDao.queryById(info.getId());
        signUpGraphicInfo.setSaveNum(signUpGraphicInfo.getSaveNum() + 1);

        int count = signUpGraphicInfoDao.updateById(signUpGraphicInfo);
        if (count == 1) {
            map.put("err_code", 200);
            map.put("err_msg", "更新成功");
        } else {
            map.put("err_code", 500);
            map.put("err_msg", "更新失败");
        }
        return map;
    }

    @Override
    public Object txAddEditNum(SignUpGraphicInfo info) {
        Map<String, Object> map = new HashMap<String, Object>();
        if (info.getId() == null) {
            map.put("err_code", 500);
            map.put("err_msg", "参数错误");
            return map;
        }
        SignUpGraphicInfo signUpGraphicInfo = signUpGraphicInfoDao.queryById(info.getId());
        signUpGraphicInfo.setEditNum(signUpGraphicInfo.getEditNum() + 1);

        int count = signUpGraphicInfoDao.updateById(signUpGraphicInfo);
        if (count == 1) {
            map.put("err_code", 200);
            map.put("err_msg", "更新成功");
        } else {
            map.put("err_code", 500);
            map.put("err_msg", "更新失败");
        }
        return map;
    }
}
